import { reactive, ref } from "vue";
import { getFriendRequestInfo, createGroup, getUserFriendList } from "../../../utils/axiosApi"
import { friendStore } from "../../../store/friendStore"
import type { GroupType, FactoryResult, PullGroupBasicInfo, UserFriendInfo } from "../../../types/types";
import { v4 as uuidv4 } from 'uuid'
import { ElMessage } from "element-plus";
import { useFactory } from "../../../utils/factory";



let result: FactoryResult = reactive({
    user_store: null,
    friend_store: null,
    chat_store: null
})


// 获取好友请求信息
// export const storageFriendRequest = ref<any>();
export function getRequestInfo() {
    // 延迟调用
    const friend_store = friendStore();

    getFriendRequestInfo().then((data): void => {
        if (data != undefined) {
            friend_store.setFriendRequestList(data);
        } else {
            // 暂无申请列表
            return;
        }
    }).catch((error): void => {
        console.error(error)
    })
}


// 存储用户好友列表
// export const storageUserFriendList = reactive<UserFriendInfo[]>([]);
export function getFriendInfo() {
    var data = getUserFriendList()
        .then((data): void => {
            if (data && typeof data === "object" && Array.isArray(data)) {
                // const valueArray = Object.values(data);
                // storageUserFriendList.splice(0, storageUserFriendList.length, ...valueArray);
                // Object.assign(storageUserFriendList, valueArray);
                // storageUserFriendList.splice(0, storageUserFriendList.length, ...data);
                friendStore().setFriendList(data);
            }
        })
        .catch((error): void => {
            console.error(error);
        });
}

// 存储transfer信息
export const infoValue = ref<any[]>([]);
// 控制显示添加群聊Dialog
export const showAddGroupChatStatus = ref<boolean>(false);
export const groupTypes: GroupType[] = reactive([
    {
        label: "学习", url: "/study", icon: "11", subTypes: [
            { label: "编程", url: "/study/programming", icon: "11a" },
            { label: "数学", url: "/study/math", icon: "11b" },
            { label: "科学", url: "/study/science", icon: "11c" }
        ]
    },
    {
        label: "工作", url: "/work", icon: "22", subTypes: [
            { label: "项目管理", url: "/work/project-management", icon: "22a" },
            { label: "开发", url: "/work/development", icon: "22b" },
            { label: "设计", url: "/work/design", icon: "22c" }
        ]
    },
    {
        label: "生活", url: "/life", icon: "33", subTypes: [
            { label: "健康", url: "/life/health", icon: "33a" },
            { label: "饮食", url: "/life/diet", icon: "33b" },
            { label: "睡眠", url: "/life/sleep", icon: "33c" }
        ]
    },
    {
        label: "娱乐", url: "/entertainment", icon: "44", subTypes: [
            { label: "电影", url: "/entertainment/movies", icon: "44a" },
            { label: "音乐", url: "/entertainment/music", icon: "44b" },
            { label: "游戏", url: "/entertainment/games", icon: "44c" }
        ]
    },
    {
        label: "运动", url: "/sports", icon: "55", subTypes: [
            { label: "跑步", url: "/sports/running", icon: "55a" },
            { label: "游泳", url: "/sports/swimming", icon: "55b" },
            { label: "篮球", url: "/sports/basketball", icon: "55c" }
        ]
    },
    {
        label: "社交", url: "/social", icon: "66", subTypes: [
            { label: "朋友", url: "/social/friends", icon: "66a" },
            { label: "家人", url: "/social/family", icon: "66b" },
            { label: "同事", url: "/social/colleagues", icon: "66c" }
        ]
    },
    {
        label: "其他", url: "/other", icon: "77", subTypes: [
            { label: "未分类", url: "/other/unclassified", icon: "77a" }
        ]
    }
]);
// 存储群成员Id信息的变量
export const groupMemberIds = ref<string[]>([]);
// 存储创建群聊基本信息的变量
export const pullGroupInfo: PullGroupBasicInfo = reactive({
    group_id: uuidv4(),
    group_name: "测试群聊1",
    group_type: "",
    group_members: [] as { key: string; label: string; avatar: string; }[] | undefined,
    group_leader: ""
})
// 拉取群聊的函数
// 拉取群聊的函数
export async function pullGroupChats(groupType: string, leaderId?: string): Promise<void> {
    const result = await useFactory();

    // 获取当前用户信息
    const UserInfo = result.user_store?.userInfo;

    // 确保当前用户信息存在
    if (!UserInfo?.uu_id || !UserInfo?.username || !UserInfo?.avatar) {
        ElMessage.error("无法获取当前用户信息");
        return;
    }

    // 清空群成员列表，避免重复添加         这个一定要加，不然群成员信息会叠加
    pullGroupInfo.group_members = [];

    // 添加群主信息到成员列表
    pullGroupInfo.group_leader = UserInfo.uu_id!;
    pullGroupInfo.group_members!.push({
        key: UserInfo.uu_id!,
        label: UserInfo.username!,
        avatar: UserInfo.avatar!
    });

    // 添加其他群成员的详细信息
    groupMemberIds.value.forEach((member_id: string) => {
        // 获取群成员基本信息
        const memberBasicInfo = friendStore().getLabelAndAvatarByUUID(member_id);
        if (memberBasicInfo) {
            // 添加到群成员列表中
            pullGroupInfo.group_members!.push(memberBasicInfo);
        }
    });

    if (groupType === 'pull') {
        // 检查群成员数量
        if (pullGroupInfo.group_members!.length < 2) {
            ElMessage.error("群成员至少需要两位！");
            return;
        }

        // 对群信息进行基本判断
        if (pullGroupInfo.group_name.trim() === "" || pullGroupInfo.group_type.trim() === "") {
            ElMessage.warning("群聊信息不完整！");
            return;
        }

        // 创建群聊
        // await createGroup(leaderId!, pullGroupInfo);
    } else {
        // 清空群聊信息，等同于重置信息
        pullGroupInfo.group_name = "";
        pullGroupInfo.group_type = "";
        pullGroupInfo.group_members = [];
        pullGroupInfo.group_leader = "";
        pullGroupInfo.group_id = uuidv4();
        infoValue.value = [];
    }
}


